package oop.core.item;
import java.util.*;
import oop.core.entity.*;
import oop.core.skill.*;
import oop.core.visitors.*;
/**
 * TrapItem implements InteractiveItem
 * Items that are interactive on the map
 * and can injure an avatar
 *
 */
public class TrapItem extends InteractiveItemVisitable
{
	private String name;
	private String imageID;
	private boolean	state;
	private String activatedImageID;
	
	/**
	 * TrapItem Constructor
	 */
	public TrapItem(String name, String imageID, boolean state, String activatedImageID)
	{
		super(name,imageID);
		this.state = state;
		this.activatedImageID = activatedImageID;
	}

	public boolean activateItem(Occupation occupation, DerivedStats derStats){
		state = true;
		List<PassiveSkill> pSkills = occupation.getPassiveSkill();
		Iterator<PassiveSkill> iter = pSkills.iterator();
		while(iter.hasNext()){
			Skill s = iter.next();
			if((s).getName().equals("RemoveTrap")){
				return activateItem(occupation, derStats, s);
			}
		}
		doDamage(derStats);
		return false;
	}
	public boolean activateItem(Occupation occupation, DerivedStats derStats, Skill skill){
		if(occupation.getName() == "Sneak"){
			if(shouldDamage(skill)){
				doDamage(derStats);
				return false;
			}
			else
			{
				return true;
			}
		}
		else{
			doDamage(derStats);
			return false;
		}
	}

	private boolean shouldDamage(Skill skill){
		int skillLevel = skill.getSkillLevel();

		// if random number(max skill level) is 
		// equal to skill level then return true
		if((int)(Math.random() * (skillLevel)) + 1 <= 3){
			return true;
		}
		return false;
	}

	private void doDamage(DerivedStats derStats){
		//mediator
		derStats.takeDamage(10);
	}

	public void setState(boolean state) {
		this.state = state;
	}

	public boolean getState() {
		return state;
	}

	public void setActivatedImageID(String ID){
		this.activatedImageID = ID;
	}

	public String getImageID(){
		return state ? this.activatedImageID : super.imageID;
	}


	public void accept(InteractiveItemVisitor visitor){
		visitor.visitTrap(this);
	}
}
